package model;
import java.util.ArrayList;
import model.MySQL.MySQLData;
import model.Sensor.MainMindThread;
import view.MainF;

public class ModelClass {
	private Variables variables;
	private MainMindThread mainMindThread;
	private ArrayList<House> houses = new ArrayList<House>();
	private ArrayList<Plant> typesOfPlants = new ArrayList<Plant>();
	private ArrayList<Measurement> measurements = new ArrayList<Measurement>();
	private House currentHouse;
	private Section currentSection;
	MainF mainF;
	private MySQLData db;
	boolean legoAddon;

	public ModelClass(MainF mainF) {
		this.mainF = mainF;
		variables = new Variables();
		mainMindThread = null;
		db = new MySQLData(this);
		db.addAllData();
		legoAddon = false;

	}

	// Method that adds house Arraylist and to the datebase depending on
	// the value of the boolean dBOrNot
	public void addHouse(int id, String name, boolean dBOrNot) {
		House house = new House(this, id, name);
		houses.add(house);
		if (dBOrNot) {
			db.uploadHouse(name);
		}
	}

	// Method that returns the highest house id
	public int getHighestHouseId() {
		int id = 0;
		id = db.getHighestHouseId();
		return id;
	}

	// Method that returns the highest section id
	public int getHighestSectionId() {
		int id = 0;
		id = db.getHighestSectionId();
		return id;
	}

	// Method that makes it possible to get a house by name
	public House getHouseName(String name) {
		for (House house : houses) {
			if (house.getName().equals(name))
				return house;
		}
		return null;
	}

	// Method that makes it possible to get a house by id
	public House getHouseId(int id) {
		for (House house : houses) {
			if (house.getId() == id)
				return house;
		}
		return null;
	}

	// Method that adds a new type of plant in Arraylist and maybe to the
	// database depending on the value of the boolean dBOrNot
	public void addPlant(Plant plant, boolean dBOrNot) {
		typesOfPlants.add(plant);
		if (dBOrNot) {
			db.uploadPlantType(plant);
		}
	}

	// Method that makes it possible to get a plant by name
	public Plant getPlantName(String name) {
		for (Plant plant : typesOfPlants) {
			if (plant.getName().equals(name))
				return plant;
		}
		return null;
	}

	// Method that makes it possible to get a plant by id
	public Plant getPlantId(int id) {
		for (Plant plant : typesOfPlants) {
			if (plant.getTypeID() == id)
				return plant;
		}
		return null;
	}

	// Method that makes it possible to get a specific section by id
	public Section getSectionId(int id) {
		for (House house : houses) {
			for (Section section : house.getSections()) {
				if (section.getId() == id) {
					return section;
				}
			}
		}
		return null;
	}

	// Adds a measurement to the measurements arraylist
	public void addMeasurements(Measurement measurement) {
		measurements.add(measurement);
	}

	// Getters and Setters
	public MainMindThread getMainMindThread() {
		return mainMindThread;
	}

	public void setMainMindThread(MainMindThread mainMindThread) {
		this.mainMindThread = mainMindThread;
	}

	public MySQLData getDb() {
		return db;
	}

	public void setDb(MySQLData db) {
		this.db = db;
	}

	public ArrayList<House> getHouses() {
		return houses;
	}

	public void setHouses(ArrayList<House> houses) {
		this.houses = houses;
	}

	public House getCurrentHouse() {
		return currentHouse;
	}

	public void setCurrentHouse(House currentHouse) {
		this.currentHouse = currentHouse;
	}

	public Section getCurrentSection() {
		return currentSection;
	}

	public void setCurrentSection(Section currentSection) {
		this.currentSection = currentSection;
	}

	public Variables getVariables() {
		return variables;
	}

	public void setVariables(Variables variables) {
		this.variables = variables;
	}

	public ArrayList<Measurement> getMeasurements() {
		return measurements;
	}

	public void setMeasurements(ArrayList<Measurement> measurements) {
		this.measurements = measurements;
	}

	public ArrayList<Plant> getTypesOfPlants() {
		return typesOfPlants;
	}

	public void setTypesOfPlants(ArrayList<Plant> typesOfPlants) {
		this.typesOfPlants = typesOfPlants;
	}

	public boolean isLegoAddon() {
		return legoAddon;
	}

	public void setLegoAddon(boolean legoAddon) {
		this.legoAddon = legoAddon;
	}
}
